<?php

namespace App\Http\Controllers\Innerend;

use App\Models\Admin;

class AdminController extends BaseController
{
    // 管理员管理
    public function initialize()
    {
        parent::initialize();

        $this->model = 'admin';
        $this->assign('title', '管理员管理');
    }

    // 列表数据展示
    protected function listTable($lists)
    {
        // 整理
        $trList = [];
        if(empty($lists)){
            return $trList;
        }

        // 展示列表表格
        $listHtml = $this->adminList();
        foreach($lists as $key => $item){
            // 数据
            $_id      = $item['id'];
            $status   = $item['status'] == 1 ? '正常' : '禁用';
            $statusOp = $item['status'] == 1 ? '禁用' : '启用';
            // 操作
            $operate = $listHtml->operate_status(['id' => $_id, 'value' => 1 - $item['status']], $statusOp, $item['status']);
            $operate .= $listHtml->operate_edit(['id' => $_id], '编辑用户');
            //$operate .= $listHtml->operate_soft_delete(['id' => $_id], '你确定要删除该用户吗？');

            // 页面
            $tr = $listHtml->list_check($item['id']);
            $tr .= $listHtml->list_td($item['username']);
            $tr .= $listHtml->list_td($item['nickname']);
            //$tr .= $this->list_td($item['role']);
            $tr .= $listHtml->list_td($status);
            $tr .= $listHtml->list_td(date('Y-m-d H:i', $item['ctime']));
            $tr .= $listHtml->list_td($operate);
            $tr .= $operate;

            // 整合
            $trList[] = $listHtml->list_tr($tr);
        }

        // 整合
        return $trList;
    }

    // 列表顶部按钮
    protected function listTopButton()
    {
        $addUrl     = Url('/innerend/admin/add');
        $top_button = '<a href="javascript:;" onclick="onEdit(\''.$addUrl.'\')" class="btn btn-success radius">添加用户</a> ';
        $this->assign('top_button', $top_button);
    }

    // 列表字段
    protected function list_fields()
    {
        // 'role' => '角色',
        return ['username' => '账户名', 'nickname' => '姓名', 'status' => '状态', 'ctime' => '创建时间', 'operate' => '操作',];
    }

    // 列表搜索
    protected function list_search()
    {
        $request  = $this->optLogic()->getData();
        $form     = $this->adminList();
        $keyword  = $form->formInput('keyword', $request['keyword'] ?? '', '关键字');
        $username = $form->formInput('username', $request['username'] ?? '', '账户名');
        $search   = ['keyword' => $keyword,];  // 'username' => $username,
        $this->assign('search', $search);

        $searchHide = ['channel'];
        $this->assign('searchHide', []);
    }

    // 表单页面
    protected function formTable()
    {
        $manage = $this->checkManage($this->model);
        if($manage === false){
            return $this->error($manage->getError());
        }

        $html = '';
        $info = $manage->onShow();
        $form = $this->adminForm();

        $adminDb = new Admin();
        $all     = $adminDb->getRole();

        // 用户名
        //$role     = $form->formCheckbox('role', json_decode($info['role']), $all); // 角色
        $status   = $form->formRadio('status', $info['status'] ?? 1, [1 => '正常', 0 => '禁用']);
        $username = $form->formInput('username', $info['username'] ?? '', '用户名');
        $nickname = $form->formInput('nickname', $info['nickname'] ?? '', '姓名');
        $password = $form->formPassword('password', '', '密码');
        $_id      = $form->formHidden('id', $info['id'] ?? '');

        //$html     .= $form->makeItem('确认密码', $password, '添加时候不填默认密码123456，修改不填默认不修改');
        $html .= $form->makeItem('用户名', $username);
        $html .= $form->makeItem('姓名', $nickname);
        $html .= $form->makeItem('密码', $password, '添加时候不填默认密码123456，修改不填默认不修改');
        $html .= $form->makeItem('状态', $status);
        $html .= $_id;

        return $html;
    }

}


